var api = require('../../utils/api.js');
var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
Page({
  data: {
    user_id: '',//用户ID
    videoURL: '',//视频地址
    pwd: '',//视频密码
    has_pwd: false,// false没有密码 true有密码
    verify_pwd: false,//判断输入密码是否正确
    data_lesson: [],//年级和学科
    seeTime: 0,//默认观看时长为0秒
    other: [],//相关课程
    video_id: '',// 视频id
    autoplay: false, //自动播放
    this_system:'' //操作系统
  },
  onLoad(params) {


    
    this.params = params;
    api.get({
      url: 'wxapp/Index/Ad_wx',
      data: {},
      success: res => {
        this.setData({
          wx_imgUrls: res
        })
      }
    });
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          videoHeight: (225/300) * parseInt(res.windowWidth)
        });
      },
      fail: function (res) { }
    });
  },
  onReady() {
    var user = wx.getStorageSync('user');//用户信息
    this.setData({
      user_id: user.id,
    });
    this.loadData();
  },
  onShow: function () {
    var that = this
    // 获取操作系统
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          this_system: res.platform
        })
      }
    })

    // Do something when page show.
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    console.log("当前时间戳为：" + timestamp);
    that.setData({
      time: timestamp
    });
  },

  //弹出框 取消按钮
  onCancel: function () {
    wx.navigateBack({ changed: true });
    // this.setData({
    //   has_pwd: false,
    //   focus: true,
    // });
  },
  //弹出框 确定按钮
  onConfirm() {
    var that = this;

    if (this.data.pwd_input == this.data.pwd) {
      this.setData({
        has_pwd: false,
        focus: true,
        autoplay:true,
      });
      // wx.navigateTo({
      //   url: '/pages/article/article?id=' + this.data.art_id
      // });
    } else {
      wx.showModal({
        title: '提示',
        content: '密码错误',
        showCancel: false,
        success: function (res) {

          if (res.confirm) {
            that.setData({
              has_pwd: true,
              focus: true
            })
          }
        }
      })
      return
    }
  },
  priceInput(e) {
    this.setData({
      pwd_input: e.detail.value
    })
  },
  loadData() {
    var that = this;
    var params = this.params;
    if (params.id) {
      wx.showNavigationBarLoading();
      api.post({
        url: 'wxapp/Index/theLesson',
        data: {
          article_id: params.id
        },
        success: data => {
          wx.hideNavigationBarLoading();
          if (data.code == 1) {
            //判断是否有密码 @bai 2018-5-25 16:12:32
            if (data.data.post_pwd) {
              this.setData({
                pwd: data.data.post_pwd,
                has_pwd: true
              })
            }

            wx.setNavigationBarTitle({
              title: data.data.post_title
            });
            
            WxParse.wxParse('postcontent','html',data.data.post_content,that,20)
            this.setData({
              article: data.data,
              videoURL: data.data.more.files[0].url,
              video_id: data.data.id,
              video_img: data.data.more.photos[0].url,
              post_pwd: data.data.post_pwd,
            });

          }
        },
        fail: err => {
          wx.hideNavigationBarLoading();
        }
      });
      //课程关联 年级和科目
      api.post({
        url: 'wxapp/Index/relevance',
        data: {
          article_id: params.id
        },
        success: res => {
          this.setData({
            grade: res.data[0].name,
            course: res.data[1].name
          })
          console.log(res.data[0].id)
          console.log(res.data[1].id)
          api.post({
            url: 'wxapp/Index/otherLessons',
            data: {
              article_id: params.id,
              grade_id: res.data[0].id,
              course_id: res.data[1].id,
            },
            success: ret => {
              this.setData({
                other: ret.data
              })
            }
          })
        }
      })
    }
  },
  onFavoriteTap(e) {
    api.post({
      url: 'user/favorites',
      data: {
        object_id: this.params.id,
        table_name: 'portal_post',
        url: JSON.stringify({ "action": "portal/Article/index", "param": { "id": this.params.id } }),
        title: this.data.article.post_title,
        description: this.data.article.post_excerpt,
      },
      success: data => {
        if (data.code) {
          wx.showToast({
            title: '收藏成功!',
            icon: 'success',
            duration: 1000
          });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'error',
            duration: 1000
          });
        }
      },
      fail: err => {
      }
    });

  },
  onLikeTap(e) {
    api.post({
      url: 'portal/articles/doLike',
      data: {
        id: this.params.id
      },
      success: data => {
        if (data.code) {
          wx.showToast({
            title: '点赞成功!',
            icon: 'success',
            duration: 1000
          });
          this.setData({ 'article.post_like': data.data.post_like });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'error',
            duration: 1000
          });
        }
      },
      fail: err => {
      }
    });
  },
  //跳转点击的课程评论页面
  onCommentTap(e) {
    wx.navigateTo({
      url: '/pages/comment/comment?object_id=' + this.params.id
    });
  },
  onShareTap() {
    wx.showShareMenu({
      withShareTicket: true
    });
  },
  previewImage(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.src, // 当前显示图片的http链接
      urls: [e.currentTarget.dataset.src] // 需要预览的图片http链接列表
    });
  },
  //分享
  onShareAppMessage(e) {
    let user_id = this.data.user_id
    let title = this.data.article.post_title
    return {
      title: title,
      path: '/pages/article/article?id=' + this.params.id,
      success: function (res) {
        api.post({
          url: 'wxapp/Index/share',
          data: {
            user_id: user_id,
            title: title
          },
          success: res => {
            wx.showToast({
              title: res.msg,
              icon: 'success',
              duration: 2000
            })
          }
        })
      },
    }
  },

  //跳转到视频列表页
  more() {
    wx.switchTab({
      url: '/pages/micro/micro'
    })
  },

  //计算观看时长
  changeSeeTime(e) {
    let that = this
    that.setData({
      seeTime: e.detail.currentTime
    })
  },

  //页面卸载(离开该页面时触发): 记录观看视频日志并更新观看累计时间
  onUnload() {
    let that = this.data
    api.post({
      url: 'wxapp/Index/studyLogs',
      data: {
        user_id: that.user_id,//用户ID
        lesson_id: this.params.id,//课程ID
        timelong: that.seeTime//观看时长
      },
      success: res => {
        console.log(res)
      }
    })
  },
});
